#include <unistd.h>
#include <fcntl.h>
#include<iostream>
#include<cerrno>
#include<cstring>
#include<unistd.h>
using namespace std;
void SetNoneBlock(int fd)
{
    int f=fcntl(fd,F_GETFL);
    if(f<0){
        perror("fcntl");
        return;
    }
    fcntl(fd,F_SETFL,f|O_NONBLOCK);
    cout<<" set "<<fd<<" nonblock done"<<endl;
}
int main()
{
    char buffer[1024];
    SetNoneBlock(0);
    sleep(1);
    while(true)
    {
        ssize_t n=read(0,buffer,sizeof(buffer)-1);
        if(n>0)
        {
            buffer[n-1]=0;
            cout<<"echo:"<<buffer<<endl;
        }
        else if(n==0)
        {
            cout<<"read done";
            break;
        }
        else{
            if(errno==EWOULDBLOCK)
            {
                cout<<"0 fd data not ready,try again"<<endl;
                sleep(5);
            }
            else{
                cout<<"read error, n="<<n<<"errno code"
                <<errno<<", errno str:"<<strerror(errno)<<endl;
            }
        }
    }
    return 0;
}